package com.express.wechat.controller;

import com.express.bean.Express;
import com.express.bean.Message;
import com.express.mvc.ResponseBody;
import com.express.mvc.ResponseView;
import com.express.service.ExpressService;
import com.express.util.JSONUtil;
import com.express.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: laixiangdong
 * @Date 2020/12/09
 */
public class QRCodeController {
    @ResponseView("/wechat/creatQRCode.do")
    public String createQRCode(HttpServletRequest req, HttpServletResponse resp) {
        String code = null;
        String userPhone = null;
        String QRCodeContent = null;
        String type = req.getParameter("type");
        if ("express".equals(type)) {
            code = req.getParameter("code");
            QRCodeContent = "express_" + code;
        } else {
            userPhone = UserUtil.getWechatUserPhone(req.getSession());
            QRCodeContent = "user_" + userPhone;
        }
        req.getSession().setAttribute("QRCode", QRCodeContent);
        System.out.println(QRCodeContent);
        return "/personQRcode.html";
    }

    @ResponseBody("/wechat/getQRCode.do")
    public String getQRCode(HttpServletRequest req, HttpServletResponse resp) {
        String QRCodeContent = (String) req.getSession().getAttribute("QRCode");
        Message message = new Message();
        if (QRCodeContent == null) {
            message.setData("取件码获取出错");
            message.setStatus(-1);
        } else {
            message.setStatus(0);
            message.setResult(QRCodeContent);
        }
        return JSONUtil.toJSON(message);
    }
}
